package com.itheima.demo2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.IntFunction;

public class StreamDemo5 {
    public static void main(String[] args) {

        /**
         * forEach： 遍历流的数据
         * count： 统计流的数据
         * toArray： 收集流中的数据 放入数组中
         *
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌-15","周芷若-18","赵敏-16","张强-20","高启强-35","张三丰-100","老莫-38","谢逊-40");

        list.stream().forEach(s -> System.out.println(s));

        long count = list.stream().count();
        System.out.println(count);

        //toArray方法的参数的作用： 负责创建一个指定类型的数组
        //toArray方法的底层：会依次得到流里面的
        String[] toArray = list.stream().toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        });

        System.out.println(Arrays.toString(toArray));


        //lambda简化
        String[] strings = list.stream().toArray(value -> new String[value]);
        System.out.println(Arrays.toString(strings));

    }
}
